package com.bianguo.android.apushdemo.view.activity;

import android.graphics.Color;
import android.support.annotation.Nullable;
import android.os.Bundle;

import com.bianguo.android.apushdemo.R;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.IMarker;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;

import java.util.ArrayList;

import butterknife.BindView;

public class ChartActivity extends BaseActivity {

    @BindView(R.id.chart_bar)
    BarChart barChartView;

    @Override
    public int layoutId() {
        return R.layout.activity_chart;
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        initBarChart();
    }

    private void initBarChart() {
        barChartView.setDrawGridBackground(false);
        barChartView.setDrawBorders(false);  //是否在折线图上添加边框
        Description desc = new Description();
        desc.setText("条形统计图");
        barChartView.setDescription(desc);// 数据描述
        barChartView.setNoDataText("暂无数据");// 如果没有数据，显示
        barChartView.setDrawGridBackground(false); // 是否显示表格颜色
        barChartView.setGridBackgroundColor(Color.WHITE); // 表格的的颜色，在这里是是给颜色设置一个透明度

        barChartView.setTouchEnabled(true); // 设置是否可以触摸
        barChartView.setDragEnabled(false);// 是否可以拖拽
        barChartView.setScaleEnabled(false);// 是否可以缩放
        barChartView.setPinchZoom(false);//
        barChartView.setDrawBarShadow(true);

        XAxis xAxis = barChartView.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return String.format("%s月", value);
            }
        });

        // 设置数据
        BarData mBarData = getBarData();
        barChartView.setData(mBarData);

        Legend mLegend = barChartView.getLegend(); // 设置比例图标示
        mLegend.setForm(Legend.LegendForm.CIRCLE);// 样式
        mLegend.setFormSize(6f);// 字体
        mLegend.setTextColor(Color.BLACK);// 颜色

        // X轴设定
        barChartView.animateY(1000);
        barChartView.invalidate();
    }

    private BarData getBarData() {
        int[] yy = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

        ArrayList<BarEntry> yValues = new ArrayList<>();
        for (int i = 0; i < yy.length; i++) {
            yValues.add(new BarEntry(yy[i], (int) (Math.random() * 20)));
        }

        // y轴的数据集合
        BarDataSet barDataSet = new BarDataSet(yValues, "BarChart Test");
        barDataSet.setColor(Color.rgb(114, 188, 223));
//        ArrayList<BarDataSet> barDataSets = new ArrayList<BarDataSet>();
//        barDataSets.add(barDataSet); // add the datasets
//        BarData barData = new BarData(xValues, barDataSets);
        BarData barData = new BarData();
        barData.addDataSet(barDataSet);
        return barData;
    }
}
